package org.wheel.plugins.toolkit.sql.capability;

import org.wheel.plugins.toolkit.sql.core.AbstractWrapper;
import org.wheel.plugins.toolkit.sql.core.WrapperContext;
import org.wheel.plugins.toolkit.sql.enums.CompareType;
import org.wheel.plugins.toolkit.sql.helper.SFunction;

/**
 * 列与半查询（子查询）或任意 SQL 片段比较能力
 *
 * @param <W> 具体 Wrapper 类型，用于链式返回自身
 * @param <T> 主实体类型
 */
public interface LambdaSemiCompare<W extends LambdaSemiCompare<W, T>, T> {

    /* ============== 默认实现 ============== */
    default W gtSub(SFunction<T,?> leftCol, String rawSqlRight){
        return compareSql(CompareType.GT, leftCol, rawSqlRight, ((AbstractWrapper<T, ?>)this));
    }
    default W geSub(SFunction<T,?> leftCol, String rawSqlRight){
        return compareSql(CompareType.GE, leftCol, rawSqlRight, ((AbstractWrapper<T, ?>)this));
    }
    default W ltSub(SFunction<T,?> leftCol, String rawSqlRight){
        return compareSql(CompareType.LT, leftCol, rawSqlRight, ((AbstractWrapper<T, ?>)this));
    }
    default W leSub(SFunction<T,?> leftCol, String rawSqlRight){
        return compareSql(CompareType.LE, leftCol, rawSqlRight, ((AbstractWrapper<T, ?>)this));
    }
    default W eqSub(SFunction<T,?> leftCol, String rawSqlRight){
        return compareSql(CompareType.EQ, leftCol, rawSqlRight, ((AbstractWrapper<T, ?>)this));
    }
    default W neSub(SFunction<T,?> leftCol, String rawSqlRight){
        return compareSql(CompareType.NE, leftCol, rawSqlRight, ((AbstractWrapper<T, ?>)this));
    }

    default <E> W exists(AbstractWrapper<E, ?> subQuery){
        return exists(true, false, subQuery, ((AbstractWrapper<T, ?>)this));
    }
    default <E> W exists(boolean condition, AbstractWrapper<E, ?> subQuery){
        return exists(condition, false, subQuery, ((AbstractWrapper<T, ?>)this));
    }
    default <E> W notExists(AbstractWrapper<E, ?> subQuery){
        return exists(true, true, subQuery, ((AbstractWrapper<T, ?>)this));
    }
    default <E> W notExists(boolean condition, AbstractWrapper<E, ?> subQuery){
        return exists(condition, true, subQuery, ((AbstractWrapper<T, ?>)this));
    }

    default <E> W inSub(SFunction<T,?> leftCol, AbstractWrapper<E, ?> subQuery){
        return inSub(true, leftCol, subQuery, ((AbstractWrapper<T, ?>)this));
    }
    default <E> W inSub(boolean condition, SFunction<T,?> leftCol, AbstractWrapper<E, ?> subQuery){
        return inSub(condition, leftCol, subQuery, ((AbstractWrapper<T, ?>)this));
    }

    default <E> W notInSub(SFunction<T,?> leftCol, AbstractWrapper<E, ?> subQuery){
        return notInSub(true, leftCol, subQuery, ((AbstractWrapper<T, ?>)this));
    }
    default <E> W notInSub(boolean condition, SFunction<T,?> leftCol, AbstractWrapper<E, ?> subQuery){
        return notInSub(condition, leftCol, subQuery, ((AbstractWrapper<T, ?>)this));
    }

    /* ============== IN / NOT IN 子查询 ============== */
    <E> W inSub(boolean condition, SFunction<T,?> leftCol, AbstractWrapper<E, ?> subQuery, AbstractWrapper<T, ?> parentWrapper);
    <E> W notInSub(boolean condition, SFunction<T,?> leftCol, AbstractWrapper<E, ?> subQuery, AbstractWrapper<T, ?> parentWrapper);

    <E> W exists(boolean condition, boolean not, AbstractWrapper<E, ?> subQuery, AbstractWrapper<T, ?> parentWrapper);
    /* ============== 原子方法 ============== */
    /**
     * 万能入口：列 vs 任意原生 SQL 片段
     * 其余 gt/ge/lt/le/eq/ne 默认转调这里
     */
    W compareSql(CompareType type, SFunction<T,?> leftCol, String rawSqlRight, AbstractWrapper<T, ?> parentWrapper);
}